package cn.threewaters.qtyy.web.controller.business;

import java.util.List;
import java.util.Map;

import javax.annotation.Resource;

import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import com.google.common.collect.Maps;

import cn.threewaters.qtyy.common.bean.ResponseJson;
import cn.threewaters.qtyy.dic.one.queryparam.SaveStartStop;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import springfox.documentation.annotations.ApiIgnore;

@Api(tags = "启动停止")
@Controller
@RequestMapping(value = "/startstop")
public class StartStopController {

	@Resource
	@Qualifier("oneJdbcTemplate")
	private JdbcTemplate oneJdbcTemplate;

	@ApiIgnore
	@ApiOperation(value = "启动停止页面")
	@RequestMapping(value = "/main", method = RequestMethod.GET)
	public ModelAndView main(ModelMap modelMap, String sblx, String orgcode, String storehousecode, String mode) {
		modelMap.put("sblx", sblx);
		modelMap.put("orgcode", orgcode);
		modelMap.put("storehousecode", storehousecode);
		modelMap.put("mode", mode);
		return new ModelAndView("business/startstop/startstop", modelMap);
	}

	@ApiOperation(value = "启动停止查询")
	@RequestMapping(value = "/list", method = RequestMethod.GET)
	@ResponseBody
	public ResponseJson query(String sblx, String orgcode, String storehousecode) throws Exception {
		Map<String, Object> modelMap = Maps.newHashMap();
		// 内环流
		if (sblx != null && sblx.equals("1")) {
			List<Map<String, Object>> queryForList = oneJdbcTemplate
					.queryForList("select * from la_inloop where orgcode = '" + orgcode + "' and storehousecode = '"
							+ storehousecode + "' order by id desc limit 1");
			if (queryForList.size() > 0) {
				String status = String.valueOf(
						queryForList.get(0).get("openstatue") == null ? 0 : queryForList.get(0).get("openstatue"));
				if (status != null && status.equals("0")) {
					status = "关闭";
				} else if (status != null && status.equals("1")) {
					status = "运行中";
				} else if (status != null && status.equals("2")) {
					status = "故障";
				} else if (status != null && status.equals("-1")) {
					status = "未知";
				}
				modelMap.put("openstatue", status);
			} else {
				modelMap.put("openstatue", "关闭");
			}
		}
		// 空调
		if (sblx != null && sblx.equals("2")) {
			List<Map<String, Object>> queryForList = oneJdbcTemplate
					.queryForList("select * from la_fan where orgcode = '" + orgcode + "' and storehousecode = '"
							+ storehousecode + "' order by id desc limit 1");
			if (queryForList.size() > 0) {
				int fancount = Integer.parseInt(String.valueOf((queryForList.get(0).get("fancount") == null
						|| String.valueOf(queryForList.get(0).get("fancount")).equals("")) ? 0
								: queryForList.get(0).get("fancount")));
				modelMap.put("count", fancount);
				for (int i = 1; i <= fancount; i++) {
					String status = String.valueOf(queryForList.get(0).get("fanstatus" + i) == null ? 0
							: queryForList.get(0).get("fanstatus" + i));
					if (status != null && status.equals("0")) {
						status = "关闭";
					} else if (status != null && status.equals("1")) {
						status = "运行中";
					} else if (status != null && status.equals("2")) {
						status = "故障";
					} else if (status != null && status.equals("-1")) {
						status = "未知";
					}
					modelMap.put("fanstatus_" + i, status);
				}
			} else {
				modelMap.put("count", 4);
				for (int i = 1; i <= 4; i++) {
					modelMap.put("fanstatus_" + i, "关闭");
				}
			}
		}
		// 通风
		if (sblx != null && sblx.equals("3")) {
			List<Map<String, Object>> queryForList = oneJdbcTemplate
					.queryForList("select * from la_aeration where orgcode = '" + orgcode + "' and storehousecode = '"
							+ storehousecode + "' order by id desc limit 1");
			if (queryForList.size() > 0) {
				int aerationcount = Integer.parseInt(String.valueOf((queryForList.get(0).get("aerationcount") == null
						|| String.valueOf(queryForList.get(0).get("aerationcount")).equals("")) ? 0
								: queryForList.get(0).get("aerationcount")));
				modelMap.put("count", aerationcount);
				for (int i = 1; i <= aerationcount; i++) {
					String status = String.valueOf(queryForList.get(0).get("aerationstatus" + i) == null ? 0
							: queryForList.get(0).get("aerationstatus" + i));
					if (status != null && status.equals("0")) {
						status = "关闭";
					} else if (status != null && status.equals("1")) {
						status = "运行中";
					} else if (status != null && status.equals("2")) {
						status = "故障";
					}
					modelMap.put("aerationstatus_" + i, status);
					modelMap.put("aerationtyep_" + i, queryForList.get(0).get("aerationtyep" + i));
				}
			} else {
				modelMap.put("count", 32);
				for (int i = 1; i <= 32; i++) {
					modelMap.put("aerationstatus_" + i, "关闭");
					modelMap.put("aerationtyep_" + i, "无类型");
				}
			}
		}
		return ResponseJson.builder().success(true).obj(modelMap).build();
	}

	@ApiOperation(value = "启动停止保存")
	@RequestMapping(value = "/save", method = RequestMethod.POST)
	@ResponseBody
	public ResponseJson save(SaveStartStop saveStart) throws Exception {
		String sql = "";
		sql = "update la_control set controlinfo = ?,ischeck = '1' where orgcode = ? and storehousecode = ? and type = ?";
		int update = oneJdbcTemplate.update(sql, saveStart.getControlinfo(), saveStart.getOrgcode(),
				saveStart.getStorehousecode(), saveStart.getSblx());
		if (update <= 0) {
			sql = "insert into la_control(orgcode,storehousecode,type,controlinfo,ischeck) values (?,?,?,?,?)";
			oneJdbcTemplate.update(sql, saveStart.getOrgcode(), saveStart.getStorehousecode(), saveStart.getSblx(),
					saveStart.getControlinfo(), "1");
		}
		return ResponseJson.builder().success(true).msg("保存成功").obj(null).build();
	}

	@ApiOperation(value = "粮情的启动保存")
	@RequestMapping(value = "/lqsave", method = RequestMethod.GET)
	@ResponseBody
	public ResponseJson saveLq(String orgcode, String storehousecode) throws Exception {
		for (int i = 0; i < storehousecode.split(",").length; i++) {
			String sql = "";
			sql = "update la_control set ischeck = '1' where orgcode = ? and storehousecode = ? and type = 0";
			int update = oneJdbcTemplate.update(sql, orgcode, storehousecode.split(",")[i]);
			if (update <= 0) {
				sql = "insert into la_control(orgcode,storehousecode,type,ischeck) values (?,?,?,?)";
				oneJdbcTemplate.update(sql, orgcode, storehousecode.split(",")[i], 0, "1");
			}
		}
		return ResponseJson.builder().success(true).msg("保存成功").obj(null).build();
	}

	@ApiOperation(value = "单击启动保存")
	@RequestMapping(value = "/clickSave", method = RequestMethod.GET)
	@ResponseBody
	public ResponseJson saveClick(String orgcode, String storehousecode, String name, String mode, String type)
			throws Exception {
		String info = "";
		if (type.equals("1")) {
			info = mode;
		} else {
			info = name + ":" + mode;
		}
		String sql = "";
		sql = "update la_control set ischeck = '1',controlinfo = ? where orgcode = ? and storehousecode = ? and type = ?";
		int update = oneJdbcTemplate.update(sql, info, orgcode, storehousecode, type);
		if (update <= 0) {
			sql = "insert into la_control(orgcode,storehousecode,controlinfo,type,ischeck) values (?,?,?,?,?)";
			oneJdbcTemplate.update(sql, orgcode, storehousecode, info, type, "1");
		}
		return ResponseJson.builder().success(true).msg("保存成功").obj(null).build();
	}

}
